import { apiConfig } from './api-config'
import type { TaskNotification } from '../../../types/task-notification'

export class TaskNotificationService {
  private baseUrl: string
  private axiosInstance: any

  constructor() {
    this.baseUrl = `/task-notification`
    this.axiosInstance = apiConfig.getAxiosInstance()
  }

  /**
   * 获取未读通知数量
   */
  async getUnreadCount(): Promise<number> {
    try {
      console.log('开始获取未读通知数量，请求URL:', `${this.baseUrl}/unread-count`)
      const response = await this.axiosInstance.get(`${this.baseUrl}/unread-count`)
      console.log('获取未读通知数量响应:', response)
      // 后端直接返回Map<String, Object>，不是包装在code/data结构中
      return response.data.unreadCount || 0
    } catch (error) {
      console.error('获取未读通知数量失败:', error)
      throw error
    }
  }

  /**
   * 获取通知列表
   */
  async getNotifications(_page = 1, _size = 10): Promise<{ list: TaskNotification[], total: number }> {
    try {
      const response = await this.axiosInstance.get(`${this.baseUrl}/all`)
      // 后端直接返回List<TaskNotification>，不是包装在code/data结构中
      const list = response.data || []
      return {
        list,
        total: list.length
      }
    } catch (error) {
      console.error('获取通知列表失败:', error)
      throw error
    }
  }

  /**
   * 获取通知详情
   */
  async getNotification(id: number): Promise<TaskNotification> {
    try {
      const response = await this.axiosInstance.get(`${this.baseUrl}/${id}`)
      // 后端直接返回TaskNotification对象，不是包装在code/data结构中
      return response.data
    } catch (error) {
      console.error('获取通知详情失败:', error)
      throw error
    }
  }

  /**
   * 标记通知为已读
   */
  async markAsRead(id: number): Promise<void> {
    try {
      // 后端API是PUT /read/{id}
      const response = await this.axiosInstance.put(`${this.baseUrl}/read/${id}`)
      // 后端直接返回Map<String, Object>，不是包装在code/data结构中
      if (!response.data.success) {
        throw new Error(response.data.message || '标记通知为已读失败')
      }
    } catch (error) {
      console.error('标记通知为已读失败:', error)
      throw error
    }
  }

  /**
   * 标记所有通知为已读
   */
  async markAllAsRead(): Promise<void> {
    try {
      // 后端API是PUT /read-all
      const response = await this.axiosInstance.put(`${this.baseUrl}/read-all`)
      // 后端直接返回Map<String, Object>，不是包装在code/data结构中
      if (!response.data.success) {
        throw new Error(response.data.message || '标记所有通知为已读失败')
      }
    } catch (error) {
      console.error('标记所有通知为已读失败:', error)
      throw error
    }
  }

  /**
   * 删除通知
   */
  async deleteNotifications(ids: number[]): Promise<void> {
    try {
      // 后端API是DELETE /{id}，需要逐个删除
      for (const id of ids) {
        const response = await this.axiosInstance.delete(`${this.baseUrl}/${id}`)
        // 后端直接返回Map<String, Object>，不是包装在code/data结构中
        if (!response.data.success) {
          throw new Error(response.data.message || '删除通知失败')
        }
      }
    } catch (error) {
      console.error('删除通知失败:', error)
      throw error
    }
  }

  /**
   * 删除已读通知
   */
  async deleteReadNotifications(): Promise<void> {
    try {
      // 后端API是DELETE /read-notifications
      const response = await this.axiosInstance.delete(`${this.baseUrl}/read-notifications`)
      // 后端直接返回Map<String, Object>，不是包装在code/data结构中
      if (!response.data.success) {
        throw new Error(response.data.message || '删除已读通知失败')
      }
    } catch (error) {
      console.error('删除已读通知失败:', error)
      throw error
    }
  }
}

// 创建全局任务通知服务实例
export const taskNotificationService = new TaskNotificationService()